package cn.edu.fjzzit.web.myhotel.controller;

import cn.edu.fjzzit.web.myhotel.config.ResultJson;

import cn.edu.fjzzit.web.myhotel.model.UserInfo;
import cn.edu.fjzzit.web.myhotel.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
//import org.springframework.web.bind.annotation.GetMapping;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;


@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")


public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation("用户登录")

    @ApiImplicitParams({

            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String", required = true),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String", required = true)

    })
    public ResultJson login(String userName, String password) {
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);
            String sessonId = subject.getSession().getId().toString();
            HashMap<String, String> map = new HashMap<>();
            map.put("token", sessonId);
            return new ResultJson("200", "登录成功", map);
        } catch (UnknownAccountException e) {

            return new ResultJson("400", "用户名或密码错误！", null);
        } catch (DisabledAccountException e) {
            return new ResultJson("400", "该账号已被禁用！", null);
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            return new ResultJson("400", "用户名或密码错误！", null);
        } catch (Exception e) {
            return new ResultJson("400", "未知错误，请联系管理员！", null);


        }

    }

    @GetMapping("/add_user")
    @ApiOperation("添加用户")

    @RequiresRoles(value = {"admin"}, logical = Logical.OR)
    @ApiImplicitParams({

            @ApiImplicitParam(name = "token", value = "鉴权用Token", dataType = "String", required = true, paramType = "header"),
            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String", required = true),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String", required = true),
            @ApiImplicitParam(name = "roleName", value = "角色名", dataType = "String", required = true)
    })
    public ResultJson addUser(String userName, String password, String roleName) {
        try {
            userService.addUser(userName, password, roleName);
            return new ResultJson("200", "添加用户成功", null);
        } catch (Exception e) {
            return new ResultJson("400", "添加用户失败", null);
        }
    }

    //    public ResultJson addUser() {
//        return  new ResultJson("200","添加用户成功",null);
//    }





    @GetMapping("/remove_user")
    @ApiOperation("删除用户")

    @RequiresRoles(value = {"admin"}, logical = Logical.OR)
    @ApiImplicitParams({

            @ApiImplicitParam(name = "token", value = "鉴权用Token", dataType = "String", required = true, paramType = "header"),
            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String", required = true),

    })
    public ResultJson removeUser(String userName) {
        try {
            userService.removeUser(userName);
            return new ResultJson("200", "删除用户成功", null);
        } catch (Exception e) {
            return new ResultJson("400", "删除用户失败" + e, null);
        }
    }

//    @RequiresRoles(value = {"admin"}, logical = Logical.OR)
//    public ResultJson resetRoles(String userName, String roleName) {
//
//    }
//
//    @RequiresRoles(value = {"admin"}, logical = Logical.OR)
//    public ResultJson disableAccount(String userName) {
//
//    }
//
//    @RequiresRoles(value = {"admin"}, logical = Logical.OR)
//    public ResultJson enableAccount(String userName) {
//
//    }
//
//    @RequiresAuthentication
//    public ResultJson findSelfUserInfo() {
//
//    }
//
//    @RequiresRoles(value = {"admin"}, logical = Logical.OR)
//    public ResultJson findAllUserInfo() {
//
//    }

    @PutMapping("/resetPassword")
    @ApiOperation("管理员重置密码")
    @RequiresRoles(value = {"admin"},logical = Logical.OR)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "鉴权用户",required = true,paramType = "header"),
            @ApiImplicitParam(name = "userName")
    })
    public ResultJson resetPassword(String userName){
        try {
            userService.resetPassword(userName);
            return new ResultJson("200","密码成功重置为123456","");
        }catch (Exception e){
            return new ResultJson("400","failed","");
        }
    }



    @GetMapping("/need_login")
    public ResultJson needLogin() {
        return new ResultJson("400", "请先登录", null);
    }

    @GetMapping("/unauth")
    public ResultJson unauth() {
        return new ResultJson("400", "你没有相应的权限", null);
    }



    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "鉴权用户", required = true, paramType = "header"),
            @ApiImplicitParam(name = "oldPassword", value = "旧密码", required = true),
            @ApiImplicitParam(name = "newPassword", value = "新密码", required = true)
    })
    @PutMapping("/resetSelfPassword")
    @ApiOperation("修改密码")
    public ResultJson resetSelfPassword(String oldPassword, String newPassword){
        //由于用户已经是登录的， 这里可以直接取出用户的信息
        Subject subject = SecurityUtils.getSubject();
        UserInfo userInfo = (UserInfo) subject.getPrincipals().getPrimaryPrincipal();
        String userName = userInfo.getUserName();
        String salt = userInfo.getSalt();

        SimpleHash simpleHash = new SimpleHash(Md5Hash.ALGORITHM_NAME,oldPassword,salt,3);
        if(!simpleHash.toString().equals(userInfo.getPassword())){
            return new ResultJson("400","修改密码失败, 原密码不匹配!",null);
        }else {
            try {
                userService.resetSelfPassword(userName,newPassword);
                return new ResultJson("200","密码修改成功!",null);
            }catch (Exception e){
                e.printStackTrace();
                return new ResultJson("400","密码修改失败",null);
            }
        }
    }

    }
